package cn.itcast.travel.service.impl;

import cn.itcast.travel.dao.FavoriteDao;
import cn.itcast.travel.dao.RouteDao;
import cn.itcast.travel.dao.RouteImgDao;
import cn.itcast.travel.dao.SellerDao;
import cn.itcast.travel.dao.impl.FavoriteDaoImpl;
import cn.itcast.travel.dao.impl.RouteDaoImpl;
import cn.itcast.travel.dao.impl.RouteImgDaoImpl;
import cn.itcast.travel.dao.impl.SellerDaoImpl;
import cn.itcast.travel.domain.PageBean;
import cn.itcast.travel.domain.Route;
import cn.itcast.travel.domain.RouteImg;
import cn.itcast.travel.domain.Seller;
import cn.itcast.travel.service.RouteService;

import java.util.List;

/**
 *
 *
 * @Author CHAI
 * @Date 2022/4/28 20:54
 **/
public class RouteServiceImpl implements RouteService {
    private RouteDao routeDao=new RouteDaoImpl();
    private RouteImgDao routeImgDao=new RouteImgDaoImpl();
    private SellerDao sellerDao=new SellerDaoImpl();
    private FavoriteDao favoriteDao=new FavoriteDaoImpl();
    @Override
    public PageBean<Route> pageQuery(int cid, int currentPage, int rows, String rname) {
        PageBean<Route> pageBean = new PageBean<Route>();
        //设置当前的页码
        pageBean.setCurrentPage(currentPage);

        //设置每页显示条数
        pageBean.setRows(rows);

        //获取总的记录数
        int totalCount = routeDao.findTotalCount(cid,rname);
        pageBean.setTotalCount(totalCount);

        //总的页码数(总共有多少页)
        int totalPage=(totalCount%rows)==0? (totalCount/rows):(totalCount/rows)+1;
        pageBean.setTotalPage(totalPage);

        /*//防止最后一页下一页越界，导致数据错误(前端进行解决)
        if (currentPage>totalPage){
            currentPage=totalPage;
        }*/

        //开始索引的位置=（ 当前的页码   -1   ）*  每页显示的条数
        int start=(currentPage-1)*rows;
        pageBean.setList(routeDao.pageQuery(cid,start,rows,rname));


        return pageBean;
    }

    @Override
    public Route findOne(int rid) {
        //根据rid赋值Route对象
        Route route = routeDao.findOne(rid);;
        //查询缩略图的集合
        List<RouteImg> img = routeImgDao.findImg(route.getRid());
        route.setRouteImgList(img);
        //查询商家对象
        Seller seller = sellerDao.findSeller(route.getSid());
        route.setSeller(seller);
        //查询收藏的数量
        int count=favoriteDao.findCount(route.getRid());
        route.setCount(count);
        return route;
    }
}
